﻿
app.controller('EmergencyController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetNodeMenu = function (node) {
        mui.showLoading("正在加载..", "div");
        var hmenu = sessionStorage.getItem('EmergencyMenu');
        if (hmenu == null) {
            $http({
                method: 'POST',
                url: '../Account/GetMMenuList',
                data: { 'Node': node }
            })
                .success(function (ret) {
                    mui.hideLoading();
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        $scope.CheckMenuList = ret.Data;
                        sessionStorage.setItem("EmergencyMenu", JSON.stringify(ret.Data));
                    }
                    else {
                        mui.toast(ret.Msg);
                    }
                })
                .error(function (data, status, headers, config) {
                    mui.toast("请检查网络");
                });
        }
        else {
            $scope.CheckMenuList = JSON.parse(hmenu);
            mui.hideLoading();
        }
    }

    $scope.AddDrillForm = function (model, EmpList) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            alertify.alert("请选择场所");
            return false;
        }
        if (model.Summary == null || model.Summary == "") {
            alertify.alert("请填写总结概要");
            return false;
        }
        if (model.DrillTime == null || model.DrillTime == "") {
            alertify.alert("填写演练时间");
            return false;
        }
        if (model.TypeId == null || model.TypeId == "") {
            alertify.alert("请选择演练类型");
            return false;
        }
        if (model.DepartmentId == null || model.DepartmentId == "") {
            alertify.alert("请选择组织部门");
            return false;
        }
        if (model.Member == null || model.Member == "") {
            alertify.alert("请填写参加人员");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../Emergency/AddDrillForm',
            data: { 'model': model, "EmployeeList": EmpList },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急演练添加成功！");

                    setTimeout(function () {
                        location.href = "DrillFormPageList";
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.EditDrillForm = function (model, page) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            alertify.alert("请选择场所");
            return false;
        }
        if (model.Summary == null || model.Summary == "") {
            alertify.alert("请填写总结概要");
            return false;
        }
        if (model.DrillTime == null || model.DrillTime == "") {
            alertify.alert("填写演练时间");
            return false;
        }
        if (model.TypeId == null || model.TypeId == "") {
            alertify.alert("请选择演练类型");
            return false;
        }
        if (model.DepartmentId == null || model.DepartmentId == "") {
            alertify.alert("请选择组织部门");
            return false;
        }
        if (model.Member == null || model.Member == "") {
            alertify.alert("请填写参加人员");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../Emergency/EditDrillForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急演练修改成功！");

                    setTimeout(function () {
                        location.href = "DrillFormPageList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.MAddDrillForm = function (model) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            mui.toast("请选择场所");
            return false;
        }
        if (model.Summary == null || model.Summary == "") {
            mui.toast("请填写总结概要");
            return false;
        }
        if (model.DrillTime == null || model.DrillTime == "") {
            mui.toast("填写演练时间");
            return false;
        }
        if (model.TypeId == null || model.TypeId == "") {
            mui.toast("请选择演练类型");
            return false;
        }
        if (model.DepartmentId == null || model.DepartmentId == "") {
            mui.toast("请选择组织部门");
            return false;
        }
        if (model.Member == null || model.Member == "") {
            mui.toast("请填写参加人员");
            return false;
        }
        $scope.Loading = true;
        model.DrillTime = $("#DrillTime").val();
        $http({
            method: 'POST',
            url: '../Emergency/AddDrillForm',
            data: { 'model': model, "FileList": $scope.FileList }
        })
            .success(function (ret) {
                $scope.Loading = false;
                if (ret.Code == 100) {
                    mui.toast("演练添加成功！");

                    setTimeout(function () {
                        location.href = "MDrillFormList";
                    }, 1000)
                } else
                    mui.toast(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.MEditEventReport = function (model) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            mui.toast("请选择场所");
            return false;
        }
        if (model.Summary == null || model.Summary == "") {
            mui.toast("请填写总结概要");
            return false;
        }
        if (model.DrillTime == null || model.DrillTime == "") {
            mui.toast("填写演练时间");
            return false;
        }
        if (model.TypeId == null || model.TypeId == "") {
            mui.toast("请选择演练类型");
            return false;
        }
        if (model.DepartmentId == null || model.DepartmentId == "") {
            mui.toast("请选择组织部门");
            return false;
        }
        if (model.Member == null || model.Member == "") {
            mui.toast("请填写参加人员");
            return false;
        }
        $scope.Loading = true;
        model.DrillTime = $("#DrillTime").val();
        $http({
            method: 'POST',
            url: '../Emergency/EditDrillForm',
            data: { 'model': model, "FileList": $scope.FileList }
        })
            .success(function (ret) {
                $scope.Loading = false;
                if (ret.Code == 100) {
                    mui.toast("演练修改成功！");

                    setTimeout(function () {
                        location.href = "MDrillFormList";
                    }, 1000)
                } else
                    mui.toast(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
    $scope.GetParkList = function () {
        $scope.ParkList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.MGetPlaceList = function (id) {
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlaceList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.toast("请检查网络");
            });
    }
    $scope.GetPlaceList = function (control, id, defaultvalue) {
        if (id == null || id == "")
            return false;
        if (defaultvalue == null) {
            $("#" + control).html($compile('<option value="" selected>请选择场所</option>')($scope));
            $("#" + control).selectpicker('refresh');
        }
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (n.Id == defaultvalue)
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                    $("#" + control).selectpicker('render');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
        if (defaultvalue != null) {
            $("#" + control).val(defaultvalue);
            $("#" + control).selectpicker('refresh');
            $("#" + control).selectpicker('render');
        }
    }



    $scope.GetDepartmentList = function (id) {
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../CheckForm/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DepartmentList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }

    $scope.GetDrillFormPageList = function (model, page) {
        $scope.DrillFormList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Emergency/GetDrillFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DrillFormList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.BindDepartmentList = function (control, defaultvalue) {
        $http({
            method: 'POST',
            url: '../HiddenTrouble/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteDrillForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Emergency/DeleteDrillForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetDrillFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.MDeleteDrillForm = function (id) {
        mui.confirm('确认删除该记录？', '系统提醒', ['否', '是'], function (e) {
            if (e.index == 1) {
                $http({
                    method: 'POST',
                    url: '../Emergency/DeleteDrillForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            $("#" + id).remove();
                        }
                        else {
                            mui.toast(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });


            } else {
                //mui.toast("");
            }
        })
    }
    $scope.MGetPlaceList = function (id) {
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlaceList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.toast("请检查网络");
            });
    }

    $scope.MGetDrillForm = function (Id) {
        $scope.DrillFormDetail = {};
        $http({
            method: 'POST',
            url: '../Emergency/GetDrillForm',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {

                    $scope.DrillFormDetail = ret.Data;
                    $scope.GetDepartmentList();
                    $scope.DrillFormDetail.ParkId = $scope.DrillFormDetail.ParkId + "";
                    $scope.DrillFormDetail.PlaceId = $scope.DrillFormDetail.PlaceId + "";
                    $scope.DrillFormDetail.TypeId = $scope.DrillFormDetail.TypeId + "";
                    $scope.DrillFormDetail.DepartmentId = $scope.DrillFormDetail.DepartmentId + "";
                    $scope.DrillFormDetail.DrillTime = $filter('JSONDate')($scope.DrillFormDetail.DrillTime, 'yyyy-MM-dd HH:mm');
                    $scope.DrillFormDetail.CreateTime = $filter('JSONDate')($scope.DrillFormDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');

                    $scope.MGetPlaceList($scope.DrillFormDetail.ParkId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetDrillFormDetail = function (Id) {
        $scope.DrillFormDetail = {};
        $http({
            method: 'POST',
            url: '../Emergency/GetDrillForm',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DrillFormDetail = ret.Data;
                    $scope.DrillFormDetail.ParkId = $scope.DrillFormDetail.ParkId + "";
                    $scope.DrillFormDetail.TypeId = $scope.DrillFormDetail.TypeId + "";
                    $scope.PlaceId = $scope.DrillFormDetail.PlaceId + "";
                    $scope.DrillFormDetail.DrillTime = $filter('JSONDate')($scope.DrillFormDetail.DrillTime, 'yyyy-MM-dd HH:mm');
                    $scope.DrillFormDetail.CreateTime = $filter('JSONDate')($scope.DrillFormDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.BindDepartmentList('dep_select', $scope.DrillFormDetail.DepartmentId);
                    $scope.GetPlaceList('placelist', $scope.DrillFormDetail.ParkId, $scope.DrillFormDetail.PlaceId);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteDrillFormFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Emergency/DeleteDrillFormFile',
                    data: { 'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            $scope.AlertBox("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeletePlanFormFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Emergency/DeletePlanFormFile',
                    data: { 'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            $scope.AlertBox("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.EditEventReport = function (model, page) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            alertify.alert("请选择场所");
            return false;
        }
        if (model.EventContent == null || model.EventContent == "") {
            alertify.alert("填写事件内容");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../EventReport/EditEventReport',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("事件报告修改成功！");
                    setTimeout(function () {
                        location.href = "EventReportPageList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.MGetDrillFormList = function (model, page) {
        if (page == 1) {
            $scope.DrillFormList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../Emergency/GetDrillFormPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.DrillFormList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitDrillFormList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetDrillFormList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetDrillFormList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.InitImgeUpload = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#imagebox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../HiddenTrouble/UploadFormImage',
                            data: { "Category": 'Emergency/DrillForm' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.FileList.push(ret.Data);
                                    console.log($scope.FileList);

                                } else
                                    alertify.alert(ret.Msg);
                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                                mui.toast("请检查网络");
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 2000);
    }

    $scope.InitImgeUpload2 = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#imagebox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../HiddenTrouble/UploadFormImage',
                            data: { "Category": 'Emergency/Evacuate' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.FileId = ret.Data;
                                    console.log($scope.FileList);

                                } else
                                    mui.toast(ret.Msg);
                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                                mui.toast("请检查网络");
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 2000);


    }

    $scope.InitImgeUpload3 = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#imagebox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../HiddenTrouble/UploadFormImage',
                            data: { "Category": 'Emergency/PlanForm' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.FileId = ret.Data;
                                    console.log($scope.FileList);

                                } else
                                    mui.toast(ret.Msg);
                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                                mui.toast("请检查网络");
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 1000);

    }


    $scope.InitDatePicker = function (control) {
        (function ($) {
            $.init();
            var btns = $(control);
            btns.each(function (i, btn) {
                btn.addEventListener('tap', function () {
                    var _self = this;
                    if (_self.picker) {
                        _self.picker.show(function (rs) {
                            jQuery(control).val(rs.text);
                            _self.picker.dispose();
                            _self.picker = null;
                        });
                    } else {
                        var optionsJson = this.getAttribute('data-options') || '{}';
                        var options = JSON.parse(optionsJson);
                        var id = this.getAttribute('id');
                        _self.picker = new $.DtPicker(options);
                        _self.picker.show(function (rs) {
                            jQuery(control).val(rs.text);
                            _self.picker.dispose();
                            _self.picker = null;
                        });
                    }
                }, false);
            });
        })(mui);
    }

    $scope.GetEmployeeList = function (empno, selecedlist) {
        $scope.EmployeeList = [];
        $http({
            method: 'POST',
            url: '../Emergency/GetEmployeeList',
            data: { "EmployeeNoName": empno, "EmployeeList": selecedlist }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EmployeeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetAllPlace = function (control, defaultvalue) {
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../Emergency/GetPlaceList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                    $("#" + control).selectpicker('render');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.SelectEmployee = function (model) {
        $scope.ChoosenEmployeeList.push(model);
        $scope.EmployeeNoList.push(model.EmployeeNo);

        for (var i = 0; i < $scope.EmployeeList.length; i++) {
            var a = $scope.EmployeeList[i];
            if (a.EmployeeNo == model.EmployeeNo) {
                $scope.EmployeeList.splice(i, 1);
            }
        }
    }

    $scope.RemoveEmployee = function (model, searchmodel) {
        for (var i = 0; i < $scope.EmployeeNoList.length; i++) {
            var a = $scope.EmployeeNoList[i];
            if (a == model.EmployeeNo) {
                $scope.EmployeeNoList.splice(i, 1);
            }
        }
        for (var i = 0; i < $scope.ChoosenEmployeeList.length; i++) {
            var a = $scope.ChoosenEmployeeList[i];
            if (a.EmployeeNo == model.EmployeeNo) {
                $scope.ChoosenEmployeeList.splice(i, 1);
            }
        }
        $scope.GetEmployeeList(searchmodel, $scope.EmployeeNoList);
    }

    $scope.AddEvacuateForm = function (model, placename,page) {
        var files = document.querySelector('input[type=file]').files;
        if (files.length == 0) {
            alertify.alert("请上传应急疏散图！");
            return false;
        }
        Upload.upload({
            method: 'POST',
            url: '../Emergency/AddEvacuateForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急疏散图添加成功！");
                    setTimeout(function () {
                        location.href = "EvacuateFileList?PlaceId=" + model.PlaceId + "&PlaceName=" + placename + "&page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.MAddEvacuateForm = function (model) {
        if ($scope.FileId == "") {
            alertify.alert("请上传应急疏散图！");
        }
        model.FileId = $scope.FileId;
        $http({
            method: 'POST',
            url: '../Emergency/MAddEvacuateForm',
            data: { 'model': model }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    mui.toast("应急疏散图添加成功！");
                    setTimeout(function () {
                          location.href = "MEvacuateFileList?PlaceId=" + model.PlaceId;
                    }, 2000)
                } else
                    mui.toast(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetEvacuateList = function (id) {
        $scope.EvacuateList = [];
        $http({
            method: 'POST',
            url: '../Emergency/GetEvacuateForm',
            data: { "PlaceId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EvacuateList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.DeleteEvacuate = function (id) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Emergency/DeleteEvacuateForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $("#" + id).remove();
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.AddPlanForm = function (model) {
        if (model.DepartmentId == null || model.DepartmentId == "") {
            alertify.alert("请选择制定部门");
            return false;
        }
        if (model.ValidDate == null || model.ValidDate == "") {
            alertify.alert("填写有效期");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        model.Diagram = $scope.FileId;
        console.log(model);
        Upload.upload({
            method: 'POST',
            url: '../Emergency/AddPlanForm',
            data: { 'model': model,'PlaceList':model.PlaceIdList},
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急预案添加成功！");

                    setTimeout(function () {
                       location.href = "PlanFormPageList?page=1";
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetPlanFormPageList = function (model, page) {
        $scope.PlanFormList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Emergency/GetPlanFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.PlanFormList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPlanFormDetail = function (Id) {
        $scope.PlanFormDetail = {};
        $http({
            method: 'POST',
            url: '../Emergency/GetPlanFormDetail',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlanFormDetail = ret.Data;
                    $scope.PlanFormDetail.DepartmentId = $scope.PlanFormDetail.DepartmentId + "";
                    $scope.BindDepartmentList('dep_select', $scope.PlanFormDetail.DepartmentId);
                    $scope.PlanFormDetail.ValidDate = $filter('JSONDate')($scope.PlanFormDetail.ValidDate, 'yyyy-MM-dd');
                    $scope.PlanFormDetail.CreateTime = $filter('JSONDate')($scope.PlanFormDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.GetAllPlace('place_select', $scope.PlanFormDetail.PlaceList);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.EditPlanForm = function (model,page) {
        if (model.DepartmentId == null || model.DepartmentId == "") {
            alertify.alert("请选择制定部门");
            return false;
        }
        if (model.ValidDate == null || model.ValidDate == "") {
            alertify.alert("填写有效期");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        if ($scope.FileId != null && $scope.FileId != '') {
            model.Diagram = $scope.FileId;
        }
        Upload.upload({
            method: 'POST',
            url: '../Emergency/EditPlanForm',
            data: { 'model': model, 'PlaceList': model.PlaceIdList },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急预案修改成功！");

                    setTimeout(function () {
                        location.href = "PlanFormPageList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.DeletePlanForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Emergency/DeletePlanForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetPlanFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }


    $scope.MGetPlanFormList = function (model, page) {
        if (page == 1) {
            $scope.PlanFormList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../Emergency/GetPlanFormPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.PlanFormList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitPlanFormList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetPlanFormList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetPlanFormList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.MDeletePlanForm = function (id) {
        mui.confirm('确认删除该记录？', '系统提醒', ['否', '是'], function (e) {
            if (e.index == 1) {
                $http({
                    method: 'POST',
                    url: '../Emergency/DeletePlanForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            $("#" + id).remove();
                        }
                        else {
                            mui.toast(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            } else {
                //mui.toast("");
            }
        })
    }
    $scope.ShowPicture = function (src) {
        $scope.IsPreview = true;
        $("#imgshow").attr("src", src);
    }
    $scope.ClosePreview = function () {
        $scope.IsPreview = false;
        $("#imgshow").attr("src", "");
    }
    $scope.MGetPlanFormDetail = function (Id) {
        $scope.PlanFormDetail = {};
        $http({
            method: 'POST',
            url: '../Emergency/GetPlanDetail',
            data: { 'Id': Id }
        }).success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlanFormDetail = ret.Data;
                    $scope.PlanFormDetail.ValidDate = $filter('JSONDate')($scope.PlanFormDetail.ValidDate, 'yyyy-MM-dd');
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEmergencyTeamPageList = function (model, page) {
        $scope.TeamList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Emergency/GetEmergencyTeamPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.TeamList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEmergencyTeamList = function () {
        $scope.TeamList = [];
        mui.showLoading("正在加载..", "div");
        $http({
            method: 'POST',
            url: '../Emergency/GetEmergencyTeamList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                mui.hideLoading();
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.TeamList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.AddEmegencyTeam = function (model, page) {
        if (model.Name == null || model.Name == "0") {
            alertify.alert("请输入姓名");
            return false;
        }
        if (model.Department == null || model.Department == "") {
            alertify.alert("请输入部门");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Emergency/AddEmergencyTeam',
            data: { 'model': model }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急队伍人员添加成功！");
                    setTimeout(function () {
                        location.href = "EmergencyTeamList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetEmergencyTeam = function (Id) {
        $scope.EditModel = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Emergency/GetEmergencyTeam',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.EditModel = ret.Data;
                    $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.UpdateEmegencyTeam = function (model, page) {
        if (model.Name == null || model.Name == "0") {
            alertify.alert("请输入姓名");
            return false;
        }
        if (model.Department == null || model.Department == "") {
            alertify.alert("请输入部门");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Emergency/UpdateEmergencyTeam',
            data: { 'model': model }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("应急队伍人员修改成功！");
                    setTimeout(function () {
                        location.href = "EmergencyTeamList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.DeleteEmegencyTeam = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Emergency/DeleteEmergencyTeam',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetEmergencyTeamPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }


    $scope.AddHandleForm = function (model, EmpList) {
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../Emergency/AddHandleForm',
            data: { 'model': model},
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("添加成功！");
                    setTimeout(function () {
                        //location.href = "DrillFormPageList";
                    }, 2000)
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetHandleFormPageList = function (model, page) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Emergency/GetHandleFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
});